package com.dm.hdfxxt.inmanage.service.impl;

import com.dm.hdfxxt.inmanage.mapper.InMapper;
import com.dm.hdfxxt.inmanage.service.InService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor=Exception.class)
public class InServiceImpl implements InService {
    @Autowired
    private InMapper inMapper;

    /**
         * @Description 查询所有经销商
         * @Author zhangxue
         * @Date 11:36 2018/10/30
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> queryAllInBound(Map<String, Object> param) {
        List<Map<String,Object>> ResultList = inMapper.queryAllInBound(param);
        Map<String,Object> countMap = inMapper.queryAllInBoundCount(param);
        int rkTotal = Integer.valueOf(countMap.get("total").toString());
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("ResultList",ResultList);
        resultMap.put("rkTotal",rkTotal);
        return resultMap;
    }

    /**
     * 保存入库
     * @param param
     * @return
     */
    @Override
    @Transactional
    public Map<String, Object> saveInBound(Map<String, Object> param) {
        Map<String,Object> resultMap = new HashMap<>();
        try {
           SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
           String NowStr1 = sdf1.format(new Date());
           param.put("CreateTime",NowStr1) ;
           param.put("RestNumber",param.get("ProductNumber"));
           int sign = 0;
           sign = inMapper.saveInBound(param);
           if (sign==1){
               sign=0;
               param.put("CreateType","del");
               param.put("DelNumber",param.get("ProductNumber"));
               if(Integer.valueOf(param.get("Type").toString())==0){
                   sign = inMapper.updatePlan(param);
                   if (sign==1){
                       resultMap.put("code",sign);
                       resultMap.put("msg","入库成功");
                       return resultMap;
                   }else {
                       TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                       resultMap.put("code",sign);
                       resultMap.put("msg","保存入库成功未修改计划,回滚");
                       return resultMap;
                   }
               }else if(Integer.valueOf(param.get("Type").toString())==1){
                   param.put("DbFlag",1);
                   sign = inMapper.updateOutDbFlag(param);
                   if (sign==1){
                       resultMap.put("code",sign);
                       resultMap.put("msg","入库成功");
                       return resultMap;
                   }else {
                       TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                       resultMap.put("code",sign);
                       resultMap.put("msg","保存入库成功未修改出库状态,回滚");
                       return resultMap;
                   }
               }else {
                   TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                   resultMap.put("code",sign);
                   resultMap.put("msg","保存失败，请选择入库类型");
                   return resultMap;
               }
           }else {
               resultMap.put("code",sign);
               resultMap.put("msg","保存入库失败未修改计划");
               return resultMap;
           }
       }catch (Exception e){
           e.printStackTrace();
           resultMap.put("code",2);
           resultMap.put("msg","服务器异常，请刷新");
           return resultMap;
       }
    }

    /**
     * 删除入库单
     * @param param
     * @return
     */
    @Override
    public Map<String, Object> delInBound(Map<String, Object> param) {
        Map<String,Object> resultMap = new HashMap<>();
        if (param.get("InNo")!=null){
            if (param.get("InNo").toString()!=null&&param.get("InNo").toString()!=""){
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
                String NowStr1 = sdf1.format(new Date());
                param.put("CreateTime",NowStr1) ;
                int sign = inMapper.delInBound(param);
                if (sign>0){
                    param.put("CreateType","add");
                    param.put("AddNumber",param.get("ProductNumber"));
                    sign =0;
                    if (Integer.valueOf(param.get("Type").toString())==0){
                        sign = inMapper.updatePlan(param);
                        if (sign>0){
                            resultMap.put("code",1);
                            resultMap.put("msg","删除成功");
                        }else {
                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                            resultMap.put("code",0);
                            resultMap.put("msg","删除入库成功,计划添加失败，回滚");
                        }
                        return resultMap;
                    }else if (Integer.valueOf(param.get("Type").toString())==1){
                        param.put("DbFlag",0);
                        sign = inMapper.updateOutDbFlag(param);
                        if (sign>0){
                            resultMap.put("code",1);
                            resultMap.put("msg","删除成功");
                        }else {
                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                            resultMap.put("code",0);
                            resultMap.put("msg","删除入库成功,出库状态失败，回滚");
                        }
                        return resultMap;
                    }else {
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        resultMap.put("code",sign);
                        resultMap.put("msg","删除失败，请刷新界面");
                        return resultMap;
                    }
                }else {
                    resultMap.put("code",0);
                    resultMap.put("msg","删除失败");
                    return resultMap;
                }
            }else {
                resultMap.put("code",0);
                resultMap.put("msg","参数错误");
                return resultMap;
            }
        } else {
            resultMap.put("code",0);
            resultMap.put("msg","未传参数");
            return resultMap;
        }
    }
}
